<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BUICategory
 *
 * @author jianfeng
 */
class BUICategory extends BUIThemedControl {

    //put your code here
    public function prepareCategoryView($owner, $control = NULL, $model = NULL) {

        //$updateControl = Blyn::app()->getUIManager()->getUpdateControl();

        if ($model == NULL)
            $model = new BDynamicFormModel();
        $categoryNameField = 'media_name_add_category';
        $categoryAliasField = 'media_alias_add_category';
        $model->addField($categoryNameField);
        $model->addField($categoryAliasField);

        $viewData = array();
        $viewData['owner'] = $owner;
        $viewData['model'] = $model;
        $viewData['categoryNameField'] = $categoryNameField;
        $viewData['categoryAliasField'] = $categoryAliasField;

        if ($owner instanceof BEntity) {
            $categoryView = BACategory::getMemberCategoryView($owner);
            $viewData['categoryView'] = $categoryView;
            $viewData['ownerClass'] = get_class($owner);
            $viewData['ownerType'] = 'BEntity';
            $viewData['ownerId'] = $owner->getId();
        }

        if ($owner instanceof BTreeList) {
            $listNode = $owner->getListNode();
            $viewData['ownerClass'] = get_class($listNode);
            $viewData['ownerId'] = $owner->getId();
            $viewData['ownerType'] = 'BTreeList';
        }

        if ($control == NULL) {
            $control = $this;
        }
        $control->addRenderView('addCategory', 'category_add', $viewData);
    }

    public function onShowAddCategory($owner, $model = NULL) {
        $updateControl = Blyn::app()->getUIManager()->getUpdateControl();
        $this->prepareCategoryView($owner, $updateControl, $model);
    }

    public function onAddCategory($data = NULL) {
        $activeLink = Blyn::app()->getUIManager()->getActiveLink();
        $ownerClass = $activeLink->getLinkVariable('ownerClass');
        $ownerId = $activeLink->getLinkVariable('ownerId');
        $ownerType = $activeLink->getLinkVariable('ownerType');
        $postManager = new BPostManager();
        $postData = $postManager->parsedModelData();

        $mediaList = $postManager->populateMediaListFromPostData($postData, 'add_category');

        $itemData = array();
        $categoryName = $itemData['name'] = $mediaList['name']->content;
        $categoryAlias = $itemData['alias'] = $mediaList['alias']->content;

        //add category
        $category = NULL;
        if ($categoryName != NULL) {
            $c = new BACategory();
            $category = $c->getRootNode()->addChild(array('name'=>$categoryName,'value'=>$categoryAlias));
            $itemData['categoryId'] = $category->getId();
        }

        //add category alias
        /*
        $aliasTable = new BACategorySheet('categoryAlias');
        $list = $aliasTable->getList(array('itemData' => array('categoryName' => $categoryName)));
        if ($list == NULL) {
            $aliasTable->addList(array('categoryName' => $categoryName, 'alias' => $categoryAlias));
        }
         * 
         */

        if ($ownerType == 'BEntity' && $ownerClass != NULL && $ownerId > 0) {
            $owner = new $ownerClass($ownerId);
        }

        if ($ownerType == 'BTreeList' && $ownerClass != NULL && $ownerId > 0) {
            $treeItem = new $ownerClass();
            $treeTable = new BTreeTable($treeItem);
            $owner = $treeTable->getListById($ownerId);
        }

        //add owner as category member
        if ($category instanceof BACategory) {
            $category->addMember($owner);
        }

        $this->onShowAddCategory($owner);
    }

    /**
     * this function use to populate model for owner from category
     * @param type $model
     * @param type $categoryTable
     */
    public function populateModelFromCategory(&$model, $owner, $categoryTable, $mediaIndex) {
        //add product category
        //$categoryTable = new BPProductSheet('productCategory', $product);
        $categoryView = $categoryTable->getView();

        if ($model instanceof BDynamicFormModel) {
            foreach ($categoryView as $id => $list) {
                $field = $this->getModelFieldByCategory($list, $mediaIndex);
                $model->addField($field);
                $name = $list->getItem('name')->getValue();
                //assign category value to model
                if ($owner instanceof BTreeList && $owner->getItem($name) != NULL) {
                    $model->$field = $owner->getItem($name)->getValue();
                }
                if ($owner instanceof BEntity) {
                    $model->$field = $owner->$name;
                }
            }
        }
    }

}
